﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;

using XmlVisualizer.Exceptions;
using XmlVisualizer.Controls;

namespace XmlVisualizer.Dialogs {
	/// <summary>
	/// Interaction logic for DialogAddElementAttribute.xaml
	/// </summary>
	public partial class DialogAddElementAttribute : Window {
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the DialogAddElementAttribute class.
		/// </summary>
		public DialogAddElementAttribute() {
			InitializeComponent();
		}

		/// <summary>
		/// Initializes a new instance of the DialogAddElementAttribute class, sets Attributes property.
		/// </summary>
		/// <param name="attributes"></param>
		/// <param name="elementName"></param>
		public DialogAddElementAttribute(AttributesList attributes, XName elementName)
			: this() {
			if (attributes != null) {
				Attributes = attributes;
			} else {
				throw new ArgumentNullException("attributes");
			}
			ElementName = elementName;
		}
		#endregion

		#region Fields
		bool posAdjusted = false;
		int returnValue;

		/// <summary>
		/// Means that window was closed successfully.
		/// </summary>
		static int IDOK = 1;

		/// <summary>
		/// Means that window canceled.
		/// </summary>
		static int IDCANCEL = -1;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the attribute list.
		/// </summary>
		public AttributesList Attributes { get; set; }

		/// <summary>
		/// Gets or sets the element name.
		/// </summary>
		public XName ElementName { get; set; }
		#endregion

		#region Methods
		/// <summary>
		/// Displays and shows the window until user closes it.
		/// </summary>
		/// <returns><see cref="DialogAddElementAttribute.IDOK">IDOK</see> on successful closure,
		/// <see cref="DialogAddElementAttribute.IDCANCEL">IDCANCEL</see> otherwise.</returns>
		public int ShowModal() {
			returnValue = IDCANCEL;
			ShowDialog(); // this "blocks" until window is closed
			return returnValue;
		}
		#endregion

		#region Event handlers
		/// <summary>
		/// Closes the window and returns <see cref="DialogAddElementAttribute.IDOK">IDOK</see>.
		/// </summary>
		/// <param name="sender">The object where the event handler is attached.</param>
		/// <param name="e">The event data.</param>
		private void Button_Click(object sender, RoutedEventArgs e) {
			returnValue = IDOK;

			XNamespace ns = tb_AttributeNamespace.Text;
			String namespacePrefix = tb_AttributeNamespacePrefix.Text;

			if (!String.IsNullOrEmpty(namespacePrefix))
				Attributes.SetValue(XNamespace.Xmlns + namespacePrefix, ns.NamespaceName);

			XName name = ns.GetName(tb_AttributeName.Text);
			String value = tb_AttributeValue.Text;

			Attributes.SetValue(name, value);
			Close();
		}

		/// <summary>
		/// Invoked when window gets activated.
		/// </summary>
		/// <param name="e">The event data.</param>
		protected override void OnActivated(EventArgs e) {
			base.OnActivated(e);
			if (!posAdjusted) {
				this.Left -= this.ActualWidth;
				this.Top -= this.ActualHeight;
				posAdjusted = true;
			}

			tb_AttributeName.Focus();
			tb_AttributeName.SelectAll();
		}
		#endregion
	}
}
